package com.tangsm.spring.boot.swagger3.controller;

import com.tangsm.spring.boot.swagger3.domain.User;
import com.tangsm.spring.boot.swagger3.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户管理")
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户信息
     * @return List<User> 用户信息集合
     */
    @ApiOperation("查询所有用户信息")
    @GetMapping( value = "/queryAll")
    public List<User> queryAll() {
        return this.userService.getAll();
    }

    /**
     * 根据姓名获取用户信息
     * @param name 姓名
     * @return User 用户信息
     */
    @ApiOperation("根据姓名获取用户信息")
    @GetMapping( value = "/queryUserByName")
    public User getByName(String name) {
        return this.userService.getByName(name);
    }

    /**
     * 添加
     * @param user 用户信息
     * @return 成功数
     */
    @ApiOperation(value = "添加用户")
    @PostMapping(value = "/add")
    public Integer add(@RequestBody User user){
        return userService.add(user);
    }

    /**
     * 删除
     * @param id 用户ID
     * @return 成功数
     */
    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(name="id", value = "用户ID")
    @DeleteMapping(value = "delete/{id}")
    public Integer delete(@PathVariable int id){
        return userService.delete(id);
    }

    /**
     * 更新
     * @param user 用户信息
     * @return 成功数
     */
    @ApiOperation(value = "更新用户")
    @ApiImplicitParam(name="id", value = "用户ID")
    @PutMapping(value = "update/{sno}")
    public Integer update(@RequestBody User user){
        return userService.update(user);
    }
}
